﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using Department.App_Code;
using System.Data;

namespace Department.admin.Content
{
    public partial class ViewAllDocuments : System.Web.UI.Page
    {
        bool wasSortedDesc = AdminVariablesSingleton.WasSortedDesc;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.IsAuthenticated)
            {
                Response.Redirect("/admin/Login.aspx");
            }
            this.Page.Title = "Всички документи";

            AdminVariablesSingleton.changeCSS(Master, "news");

            Control foundControl = AdminVariablesSingleton.FindControlRecursive(Page.Master, "viewDocuments");
            ((HtmlControl)foundControl).Attributes.Add("class", "sub_show");
        }

        protected void markInactiveButton_Click(object sender, CommandEventArgs e)
        {
            string ID = e.CommandArgument.ToString();

            markAnItemInactive(ID);

            itemsView.DataBind();

        }

        private static void markAnItemInactive(string ID)
        {
            string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DepartmentConnectionString"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(strConnString))
            {
                try
                {
                    conn.Open();

                    //Get Site content
                    string sqlText = "UPDATE Documents SET IsHidden=1 WHERE ID = @ID";
                    SqlCommand comText = new SqlCommand(sqlText, conn);
                    comText.Parameters.AddWithValue("@ID", ID);

                    comText.ExecuteNonQuery();
                }

                catch (SqlException sqle)
                {                     
                    sqle.ToString();
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        protected void sortByHeading_Click(object sender, CommandEventArgs e)
        {
            string item = e.CommandArgument.ToString();

            string sqlText;
            if (wasSortedDesc)
            {
                sqlText = itemsViewDataSource.SelectCommand + " ORDER BY " + item + " DESC";
                AdminVariablesSingleton.WasSortedDesc = false;
            }
            else
            {
                sqlText = itemsViewDataSource.SelectCommand + " ORDER BY " + item;
                AdminVariablesSingleton.WasSortedDesc = true;
            }

            itemsViewDataSource.SelectCommand = sqlText;
            itemsView.DataBind();

        }

        protected void toggleall_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkHeader = (CheckBox)sender;
            for (int i = 0; i < itemsView.Items.Count; i++)
            {
                CheckBox chk = (CheckBox)itemsView.Items[i].FindControl("CheckBoxIsMarked");
                chk.Checked = chkHeader.Checked;
            }
        }
        protected void markAllInactive_Click(object sender, CommandEventArgs e)
        {
            foreach (ListViewItem item in itemsView.Items)
            {
                CheckBox isSelectedCheckBox = (CheckBox)itemsView.Items[item.DisplayIndex].FindControl("CheckBoxIsMarked");
                if (isSelectedCheckBox.Checked)
                {
                    markAnItemInactive(((Label)item.FindControl("IDLabel")).Text);
                }
            }
            itemsView.DataBind();
        }

        public void FilterButton_Click(object sender, EventArgs e)
        {
            string date;
            string text;
            string name;

            if (FilterTitleTextBox.Text.Equals("") && FilterDateTextBox.Text.Equals("") && FilterDescTextBox.Text.Equals(""))
            {
                FilterLabel.Text = "Моля, изберете поле за филтриране!";
                FilterLabel.Visible = true;
            }
            else
            {

                date = FilterDateTextBox.Text.ToString();
                text = FilterDescTextBox.Text.ToString();
                name = FilterTitleTextBox.Text.ToString();

                DataTable dt = new DataTable();

                dt = CallStoredProcedure.FilterDocuments(name, date, text);
                itemsView.DataSourceID = null;
                itemsView.DataSource = dt;
                if (!(dt.Rows.Count == 0))
                {
                    itemsView.DataBind();
                    UpdateContentPanel.Update();

                }

            }

        }

    }
}